<template>
  <div class="main">
    <el-container>
      <!--      头部区域-->
      <el-header>
        <div>
          <img src="./../../assets/logo.png" alt="电商管理系统">
          <h3>电商后台管理系统</h3>
        </div>
        <div>
          <span>欢迎您，【{{user.username}}】</span>
          <el-button type="info" @click="logout">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <!--        左侧导航栏-->
        <el-aside :class="isShow ? '' : 'menu_hidden'">

          <div class="shrink_box">
            <el-tooltip
              effect="dark"
              :content="isShow ? '收起菜单' : '展开菜单'"
              placement="top"
              :open-delay="200"
            >
              <i :class="isShow ? 'iconfont icon-indent-left' : 'iconfont icon-indent-right'"
                 @click="changeMenuShowStatus"></i>
            </el-tooltip>
          </div>

          <el-menu background-color="#333744"
                   text-color="#fff"
                   active-text-color="#2bd8ff"
                   :collapse="!isShow"
                   :collapse-transition="false"
                   unique-opened
                   router
                   :default-active="activePath"
          >
            <el-submenu v-for="oneLevel in menus" :key="oneLevel.id" :index="oneLevel.id + ''">
              <template slot="title">
                <i :class="iconObj[oneLevel.id]"></i>
                <span>{{oneLevel.authName}}</span>
              </template>
              <el-menu-item v-for="twoLevel in oneLevel.children" :key="twoLevel.id" :index="'/' + twoLevel.path"
                            @click="saveNavState('/' + twoLevel.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{twoLevel.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>

        </el-aside>
        <!--        主体内容部分-->
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data: () => {
    return {
      user: {},
      menus: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-permission',
        101: 'iconfont icon-goods',
        102: 'iconfont icon-order',
        145: 'iconfont icon-statistics'
      },
      isShow: true,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    this.user = JSON.parse(window.sessionStorage.getItem('user'))
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  methods: {
    // 保存选中菜单的值
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 缩放菜单
    changeMenuShowStatus () {
      this.isShow = !this.isShow
    },

    // 获取导航栏菜单
    async getMenus () {
      const { data: res } = await this.$axios.get('menus')
      this.menus = res.data
      window.sessionStorage.setItem('menus', JSON.stringify(this.menus))
    },
    // 退出
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    }
  }
}
</script>
<style lang="less" scoped>
  @import "./index";
</style>
